package com.mes.cloud.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mes.cloud.dao.BmeBcDesignDAO;
import com.mes.cloud.dto.BmeBcDesignDTO;
import com.mes.cloud.service.BmeBcDesignService;
import com.mes.cloud.util.CommPager;
import com.mes.cloud.util.UuId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* h5活动设计
* @author wxm
* @Time 2020-5-12
*/
@Service
public class BmeBcDesignServiceImpl implements BmeBcDesignService {
    @Autowired
    private BmeBcDesignDAO bmeBcDesignDAO;

    @Override
    public List<BmeBcDesignDTO> getList(Map<Object, Object> condition) {
        return bmeBcDesignDAO.getList(condition);
    }

    @Override
    public long getCount(Map<Object, Object> condition) {
        return bmeBcDesignDAO.getCount(condition);
    }

    @Override
    public long insert(BmeBcDesignDTO bmeBcDesignDTO) {
        Map<Object, Object> condition = new HashMap<Object, Object>();
        condition.put("code", bmeBcDesignDTO.getCode());
        //如果存在
        if (exists(condition)) {
            throw new RuntimeException("code exists!");
        }
        //生成主键
        bmeBcDesignDTO.setId(UuId.getShortUuid());
        return bmeBcDesignDAO.insert(bmeBcDesignDTO);
    }

    public boolean exists(Map<Object, Object> condition) {
        boolean returnB = false;
        long count = getCount(condition);
        if (count > 0) {
            returnB = true;
        }
        return returnB;
    }

    @Override
    public long update(BmeBcDesignDTO bmeBcDesignDTO) {
        return bmeBcDesignDAO.update(bmeBcDesignDTO);
    }

    @Override
    public long delete(String id) {
        return bmeBcDesignDAO.delete(id);
    }

    @Override
    public BmeBcDesignDTO get(String id) {
        return bmeBcDesignDAO.get(id);
    }

    @Override
    public BmeBcDesignDTO getByCode(String code) {
        return bmeBcDesignDAO.getByCode(code);
    }

    @Override
    public CommPager<BmeBcDesignDTO> getPageList(Map<Object, Object> condition) {
        int pageIndex = condition.get("pageIndex") == null ? 0 : Integer.valueOf(condition.get("pageIndex").toString());
        int pageSize = condition.get("pageSize") == null ? 10 : Integer.valueOf(condition.get("pageSize").toString());
        Page<BmeBcDesignDTO> page = PageHelper.startPage(pageIndex, pageSize, true);
        List<BmeBcDesignDTO> dictList = bmeBcDesignDAO.getList(condition);
        CommPager<BmeBcDesignDTO> pageResult = new CommPager<>(page, dictList);
        return pageResult;
    }

    @Override
    public long batchDelete(Map<Object, Object> map) {
        if (map.get("ids") == null) {
            throw new RuntimeException("ids不能为空");
        }
        return bmeBcDesignDAO.batchDelete((List<String>) map.get("ids"));
    }

    @Override
    public long enable(Map<Object, Object> map) {
        if (map.get("id") == null || map.get("isEnable") == null) {
            throw new RuntimeException("参数异常");
        }
        return bmeBcDesignDAO.enable(map);
    }
}
